package com.xbongbong.pro.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 套餐类型
 * 创建时间 2019/5/6 4:45 PM
 * 修改时间 2019/5/6 4:45 PM
 * @author chy
 * @since v1.0
 * @version v1.0
 */
@Getter
public enum PackageTypeEnum {
    /**
     * 公司级别基础套餐相关，会冗余到company 表feeType字段内
     * 注：如果公司级别增加新的套餐类型，需要在下面方法getLowFeeTypeList内做对应处理
     */
    FREE(-1, "free", "enum.packageType.name.free"),
    //0和1为saas中原有套餐，默认为现在的专业版，之后pro的专业版的套餐都为1
    STANDARD(0, "standard", "enum.packageType.name.standard"),
    ADVANCED(1, "advanced", "enum.packageType.name.advanced"),
    //pro旗舰版
    ULTIMATE(3, "ultimate", "enum.packageType.name.ultimate"),

    /**
     * 【非】公司级别基础套餐相关，只是基础套餐，【不会】冗余到company 表feeType字段内
     */
    //短信套餐按以前逻辑，不走缓存
    SMS(ValueAddedTypeEnum.SMS.getType(),  "sms", "enum.packageType.name.sms"),

    //增值套餐 走缓存逻辑
    FORM(ValueAddedTypeEnum.ONE_FORM_DATA_NUM.getType(), "form", "enum.packageType.name.form"),

    CAPACITY(ValueAddedTypeEnum.CAPACITY_NUM.getType(), "capacity", "enum.packageType.name.capacity"),

    //自定义表单
    CUSTOM_FORM_NUM(ValueAddedTypeEnum.CUSTOM_FORM_NUM.getType(),  "customFormNum", "enum.packageType.name.custom_form_num"),
    //自定义图表
    CUSTOM_CHART_NUM(ValueAddedTypeEnum.CUSTOM_CHART_NUM.getType(), "customChartNum", "enum.packageType.name.custom_chart_num"),
    //智能助手
    AI_ASSISTANT(ValueAddedTypeEnum.AI_ASSISTANT.getType(), "aiAssistant", "enum.packageType.name.ai_assistant"),
    //api接口
    API(ValueAddedTypeEnum.API.getType(), "api", "enum.packageType.name.api"),

    SEARCH_CUSTOMER(ValueAddedTypeEnum.SEARCH_CUSTOMER.getType(), ValueAddedTypeEnum.SEARCH_CUSTOMER.getAlias(), "enum.packageType.name.search_customer"),

    BUSINESS_SEARCH(ValueAddedTypeEnum.BUSINESS_SEARCH.getType(), ValueAddedTypeEnum.BUSINESS_SEARCH.getAlias(), "enum.packageType.name.business_search"),

    BUSINESS_RULES(ValueAddedTypeEnum.BUSINESS_RULES.getType(), ValueAddedTypeEnum.BUSINESS_RULES.getAlias(), "enum.packageType.name.business_rules"),

    ESIGN(ValueAddedTypeEnum.ESIGN.getType(), ValueAddedTypeEnum.ESIGN.getAlias(), "enum.packageType.name.esign"),

    SERVICE(ValueAddedTypeEnum.SERVICE.getType(), ValueAddedTypeEnum.SERVICE.getAlias(), "enum.packageType.name.service"),

    XBB_CALL(ValueAddedTypeEnum.XBB_CALL.getType(), ValueAddedTypeEnum.XBB_CALL.getAlias(), "enum.packageType.name.xbb_call"),

    DATA_KANBAN(ValueAddedTypeEnum.DATA_KANBAN.getType(), ValueAddedTypeEnum.DATA_KANBAN.getAlias(), "enum.packageType.name.data_kanban"),

    CLOUD(ValueAddedTypeEnum.CLOUD.getType(), ValueAddedTypeEnum.CLOUD.getAlias(), "enum.packageType.name.cloud"),

    INCREMENT(ValueAddedTypeEnum.INCREMENT.getType(), ValueAddedTypeEnum.INCREMENT.getAlias(), "enum.packageType.name.increment"),
    DISTRIBUTOR(ValueAddedTypeEnum.DISTRIBUTOR.getType(), ValueAddedTypeEnum.DISTRIBUTOR.getAlias(), "enum.xbbRefType.name.distributor"),

    WORKFLOW(ValueAddedTypeEnum.WORKFLOW.getType(), ValueAddedTypeEnum.WORKFLOW.getAlias(), "enum.xbbRefType.name.distributor"),

    HOME_PAGE(ValueAddedTypeEnum.HOME_PAGE.getType(), ValueAddedTypeEnum.HOME_PAGE.getAlias(), "enum.packageType.name.home_page"),

    CONTRACTCOMPARE(ValueAddedTypeEnum.CONTRACT_COMPARE.getType(), ValueAddedTypeEnum.CONTRACT_COMPARE.getAlias(), "enum.packageType.name.contractComapre"),

    DATA_SET(ValueAddedTypeEnum.DATA_SET.getType(), ValueAddedTypeEnum.DATA_SET.getAlias(), "enum.packageType.name.data_set"),

    //新工单标准版
    WORK_ORDER_V2(ValueAddedTypeEnum.WORK_ORDER_V2.getType(), ValueAddedTypeEnum.WORK_ORDER_V2.getAlias(), ValueAddedTypeEnum.WORK_ORDER_V2.getName()),



    UNKNOWN(404, "unknown", "enum.packageType.name.unknown"),
    ;

    /**
     * 套餐类型
     */
    private int type;

    /**
     * 应用模板类型名称
     */
    private String alias;
    /**
     * 应用模板类型名称
     */
    private String name;

    PackageTypeEnum(int type, String alias, String name) {
        this.type = type;
        this.alias = alias;
        this.name = name;
    }

    public static PackageTypeEnum getByType(Integer type) {
        for (PackageTypeEnum cache : values()) {

            if (Objects.equals(type, cache.getType())) {
                return cache;
            }
        }
        return UNKNOWN;
    }


    /**
     * 是否专业版套餐
     * @param type
     * @return
     * 创建时间 2019/7/6 7:37 PM
     * 修改时间 2019/7/6 7:37 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    public static boolean isProfessionalPackage(Integer type){
        if (Objects.equals(type, STANDARD.getType()) || Objects.equals(type, ADVANCED.getType())) {
            return true;
        }
        return false;
    }

    public static List<PackageTypeEnum> getValueAddedEnums() {
        List<PackageTypeEnum> valueAddedEnums = new ArrayList<>();
        valueAddedEnums.add(FORM);
        valueAddedEnums.add(CAPACITY);
        valueAddedEnums.add(CUSTOM_CHART_NUM);
        return valueAddedEnums;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }

    public static PackageTypeEnum getByAlias(String alias) {
        for (PackageTypeEnum packageTypeEnum : values()) {
            if (Objects.equals(alias, packageTypeEnum.getAlias())) {
                return packageTypeEnum;
            }
        }
        return UNKNOWN;
    }

    public static String getNameByType(Integer type) {
        PackageTypeEnum packageTypeEnum = getByType(type);
        if (Objects.isNull(packageTypeEnum) || Objects.equals(PackageTypeEnum.UNKNOWN, packageTypeEnum)) {
            packageTypeEnum = PackageTypeEnum.STANDARD;
        }
        return packageTypeEnum.getName();
    }

    /**
     * 根据套餐类型获取当前套餐及低于当前套餐的套餐集合
     * 比如，只能旗舰版套餐肯定能看到高级版、标准版、免费版所有的功能
     * @param feeType 套餐类型
     * @return java.util.List<java.lang.Integer>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public static List<Integer> getLowFeeTypeList(Integer feeType) {
        List<Integer> lowFeeTypeList = new ArrayList<>();
        //当前套餐塞入
        lowFeeTypeList.add(feeType);
        if (Objects.equals(feeType, STANDARD.type)) {
            //标准版包含免费版
            lowFeeTypeList.add(FREE.type);
            return lowFeeTypeList;
        } else if (Objects.equals(feeType, ADVANCED.type)) {
            //高级版包含免费版、标准版
            lowFeeTypeList.add(FREE.type);
            lowFeeTypeList.add(STANDARD.type);
            return lowFeeTypeList;
        } else if (Objects.equals(feeType, ULTIMATE.type)) {
            //旗舰版包含免费版、标准版、高级版
            lowFeeTypeList.add(FREE.type);
            lowFeeTypeList.add(STANDARD.type);
            lowFeeTypeList.add(ADVANCED.type);
            return lowFeeTypeList;
        }
        return lowFeeTypeList;
    }
    /**
     * 判断套餐是否是免费版或标准版
     * @param feeType
     * @return boolean true代表免费版或标准版套餐
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public static boolean checkFreeOrStandard(Integer feeType){
        return Objects.equals(feeType, FREE.type) || Objects.equals(feeType, STANDARD.type);
    }
}
